package se.hrbustrc.hlgshopping.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import se.hrbustrc.hlgshopping.entity.PCTreeNode;
import se.hrbustrc.hlgshopping.entity.PCategory;
import se.hrbustrc.hlgshopping.service.PCategoryService;
import se.hrbustrc.hlgshopping.utils.Result;
import se.hrbustrc.hlgshopping.service.ProductService;

import java.util.List;

@RestController
@RequestMapping("/pcate")
public class PCategoryController {
    @Autowired
    PCategoryService pCategoryService;
    
    // 注入ProductService
    @Autowired
    ProductService productService;
    
    @RequestMapping("/get/{id}")
    public Result getPCategoryById(@PathVariable("id") int id)
    {
        Result result =new Result();
        result.setCode(200);
        result.setMsg("成功");
        result.setData(pCategoryService.getPCategoryById(id));
        return result;
    }
    @RequestMapping("/list/{id}")
    public Result getTreeNode(@PathVariable("id") int id){
        Result result =new Result();
        return result.ok(pCategoryService.getPCTreeList(id));
    }
    @RequestMapping("/add")
    public Result addPCategory(PCategory pCategory) {
        Result r=new Result();
        r.setCode(200);
        r.setMsg("成功");
        pCategoryService.addPCategory(pCategory);
        return r;
    }
    /*
     *修改分类
     */
    @RequestMapping("/edit")
    public Result editPCategory(PCategory pCategory) {
        Result r=new Result();
        r.setCode(200);
        r.setMsg("成功");
        pCategoryService.editPCategory(pCategory);
        return r;
    }
    /*
     *删除分类
     */
    @RequestMapping("/delete/{id}")
    public Result deletePCategory(@PathVariable("id") int id) {
        Result r=new Result();
        r.setCode(200);
        r.setMsg("成功");
        pCategoryService.deletePCategory(id);
        return r;
    }
    /*
     *批量删除分类
     */
    @RequestMapping("/delete")
    public Result deletePCategoryBatch(String[] ids) {
        Result r=new Result();
        r.setCode(200);
        r.setMsg("成功");
        for (String id:ids){
            pCategoryService.deletePCategory(Integer.parseInt(id));
        }

        return r;
    }
    //返回父级
    /*
     * 获取指定分类的顶级父级分类树
     */
    @RequestMapping("/parent/{id}")
    public Result getParentCategoryById(@PathVariable("id") int id) {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("成功");
        result.setData(pCategoryService.getParentChainById(id));
        return result;
    }
    
    /*
     * 根据分类ID获取商品列表（包括所有子分类的商品）
     */
    @RequestMapping("/products/all/{id}")
    public Result getProductsByCategoryIdAndChildren(@PathVariable("id") int categoryId) {
        Result result = new Result();
        try {
            // 获取分类及其所有子分类的ID
            List<Integer> categoryIds = pCategoryService.getAllCategoryIdsById(categoryId);
            
            // 根据分类ID列表查询商品
            result.setCode(200);
            result.setMsg("成功");
            result.setData(productService.getProductsByCategoryIds(categoryIds));
            
            return result;
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("查询失败: " + e.getMessage());
            return result;
        }
    }
}
